﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Things.OrderPlan.Models;

namespace Things.OrderPlan
{
    public class SetParameterRepository
    {
        public bool Store(SetParameter setParameter)
        {
            using (Models.ModelsContainer container = new ModelsContainer())
            {
                if (container.SetParameterSet.Where(p=>p.GoodsId == setParameter.GoodsId).Count()>0)
                {
                    container.SetParameterSet.Attach(setParameter);
                    container.ObjectStateManager.ChangeObjectState(setParameter, System.Data.EntityState.Modified);
                }
                else
                {
                    container.SetParameterSet.AddObject(setParameter);
                }
                if (container.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public SetParameter Find(Guid goodsId)
        {
            using (Models.ModelsContainer container = new ModelsContainer())
            {
                return container.SetParameterSet.Where(p=>p.GoodsId==goodsId).FirstOrDefault();
            }
        }
    }
}
